package cn.gd.controller.systemAdministration.userManagement;

import cn.gd.pojo.Organizationstructure;
import cn.gd.pojo.Roles;
import cn.gd.pojo.Users;
import cn.gd.service.systemAdministration.User.UserService;
import cn.gd.utils.EncryptionUtil;
import cn.gd.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService service;

    /**
     * 查询用户
     * @param pageNum 分页页数
     * @param pageSize 每页数量
     * @param user 模糊查询
     * @return
     */
    @PostMapping("/queryUser/{pageNum}/{pageSize}")
    public Result<PageInfo<Users>> queryUser(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            @RequestBody(required = false) Users user) {
        return Result.ok(service.queryUsers(pageNum, pageSize, user));
    }

    /**
     * 根据ID删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/deleteUserByUserId")
    public Result<Boolean> deleteUserByUserId(Integer userId) {
        return Result.ok(service.deleteUserByUserId(userId));
    }

    /**
     * 根据ID查询用户
     * @param userId
     * @return
     */
    @GetMapping("/queryUserByUserId")
    public Result<Map<String,Object>> queryUserByUserId(Integer userId) {
        Users users = service.queryUserByUserId(userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("userID", users.getUserID());
        map.put("userName", users.getUserName());
        map.put("password", users.getPassword());
        map.put("realName", users.getRealName());
        map.put("organizationStructure",new Integer[]{
                users.getOrganizationStructure().getOrgID(),
                users.getOrganizationStructure().getDepartmentss().getDeptID(),
        });
        map.put("gender", users.getGender());
        map.put("title", users.getTitle());
        map.put("roles", users.getRoles().getRoleID());
        map.put("status", users.getStatus());
        return Result.ok(map);
    }

    /**
     * 查询角色
     * @return
     */
    @GetMapping("/queryRole")
    public Result<List<Roles>> queryRole(){
        return Result.ok(service.queryRole());
    }

    @GetMapping("/queryOrganization")
    public Result<List<Organizationstructure>> queryOrganization(){
        return Result.ok(service.queryOrganization());
    }

    /**
     * 修改用户根据ID
     * @param user
     * @return
     */
    @PutMapping("/updateUser")
    public Result<Boolean> updateUser(@RequestBody Users user) throws NoSuchAlgorithmException {
        user.setPassword(EncryptionUtil.hashPassword(user.getPassword()));
        return Result.ok(service.updateUserByUserID(user));
    }

    @PostMapping("/addUser")
    public Result<Boolean> addUser(@RequestBody Users users) throws NoSuchAlgorithmException {
        users.setPassword(EncryptionUtil.hashPassword(users.getPassword()));
        return Result.ok(service.addUser(users));
    }
}
